﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ClassLibrary.DAO;
using ClassLibrary.Entity;
using System.IO;

public partial class Admin_ucControl_ucAddMenu : System.Web.UI.UserControl
{
    TblTypeDAO typeDAO = new TblTypeDAO(Global.CONNECTION_STRING);
    TblMenuDAO menuDAO = new TblMenuDAO(Global.CONNECTION_STRING);
    TblImageMenuDAO imageMenuDAO = new TblImageMenuDAO(Global.CONNECTION_STRING);
  
    protected void Page_Load(object sender, EventArgs e)
    {
       
        if (!IsPostBack)
        {
            loadTypeMenu();
            loadDetail();
            string id = Request.QueryString["id"];

            if (id == "" || id == null)
            {
                loadTypeMenu();
            }
            else
            {
                loadDetail();
                LoadImageMenu();
            }
        }
    }

    private void loadDetail()
    {
        string id = Request.QueryString["id"];
         if (id == null)
         {
         }
         else
         {
             DataTable dt = menuDAO.GetMenuByID(Convert.ToInt32(id));
             foreach (DataRow dr in dt.Rows)
             {
                 drIDType.SelectedValue = dr["IDType"].ToString();
                 txtListMenu.Content = dr["ListMenu"].ToString();
                 txtMenuName.Text = dr["MenuName"].ToString();
                 txtPrice.Text = dr["Price"].ToString();
             }
         }

    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        string id = Request.QueryString["id"];
        bool flag = false;
        try
        {
            TblMenu menu = new TblMenu();
            menu.IDType = Global.ConvertToInt(drIDType.SelectedValue);
            menu.ListMenu = Server.HtmlDecode(txtListMenu.Content);
            menu.MenuName = txtMenuName.Text;
            menu.Price = Convert.ToDecimal(txtPrice.Text);

            if (id == null || id == "")
            {
                menu.IDMenu = 0;
                menuDAO.InSert_Up(menu, 1);
            }
            else
            {
                menu.IDMenu = Global.ConvertToInt(id) ;
                menuDAO.InSert_Up(menu, 2);
            }

            flag = true;
        }
        catch (Exception ex)
        {
            throw ex;
        }
        if (flag)
        {
            ScriptManager.RegisterStartupScript(btnSave, btnSave.GetType(), "alert", "alert(' Insert Sucessful!');", true);
            //Response.Redirect("~/ListMenu.aspx");
        }
    }

    private void loadTypeMenu()
    {
        drIDType.DataSource = typeDAO.GetAll();
        drIDType.DataTextField = "TypeName";
        drIDType.DataValueField = "IDType";
        drIDType.DataBind();
    }

    private void LoadImageMenu()
    {
        string id = Request.QueryString["id"];
        grdImg.DataSource = menuDAO.GetImageMenu(Convert.ToInt32(id));
        grdImg.DataBind();
    }
    protected void btnDelImg_Click(object sender, ImageClickEventArgs e)
    {
        string id = Request.QueryString["id"];
        bool flag = false;
        try
        {
            TblImageMenu ImageMenu = new TblImageMenu();

            ImageMenu.IDMenu = Convert.ToInt32(((ImageButton)sender).CommandArgument);
            int idXoa = Convert.ToInt32(((ImageButton)sender).CommandArgument);
            ImageMenu = imageMenuDAO.GetImageMenuByID(Global.ConvertToInt(id), idXoa);
            menuDAO.DeleteImgMenu(ImageMenu);

            string filename = ImageMenu.ImageURL;
            if (File.Exists(Server.MapPath("~\\Admin\\img\\ImageMenu\\" + filename)))
            {
                File.Delete(Server.MapPath("~\\Admin\\img\\ImageMenu\\" + filename));
            }

            flag = true;
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE);
        }
        if (flag)
        {
            LoadImageMenu();
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string id = Request.QueryString["id"];
        bool flag = false;
        try
        {

            string path = Server.MapPath("~\\Admin\\img\\ImageMenu\\");
            string filename = id + "_" + fileUpload1.FileName;

            TblImageMenu imgMenu = new TblImageMenu();
            // lblMessage.Text = path;
            if (File.Exists(path + "\\" + filename))
            {
                lblMessage.Text = "The image file already exist. Please choose other file or change the file name to upload";
                return;
            }
            fileUpload1.SaveAs(path + "\\" + filename);
            imgMenu.ImageURL = filename;
            imgMenu.IDMenu = Convert.ToInt32(id);

            menuDAO.InsImageMenu(imgMenu, 1);
            flag = true;
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE);
        }
        if (flag)
        {
            //if (id == null || id == "")
            //    Response.Redirect("~/Admin/NewMenu.aspx?id=" + id);
            //else
               
                LoadImageMenu();
        }
    }
}


